import pita, pita.log
import pita.config as cfg
import logging, os

logger = logging.getLogger('pita.app')

class Application:
    def __init__(self, name):
        self.name = name
        
        pita.conf.add_option(
            cfg.Option('port',
                       '-p',
                       '--port',
                       ['pitad', 'port'],
                       10001,
                       int,
                       help='port on which daemon listens'))

        pita.conf.add_option(
            cfg.BoolOption('verbose',
                           '-v',
                           '--verbose',
                           ['pitaweb', 'verbose'],
                           False))

    def start(self):
        args = pita.conf.read(config_files = ['/etc/pita.conf',
                                                os.path.expanduser('~/.pitarc')],
                                usage = self.usage())
        if pita.conf.verbose:
            level = logging.DEBUG
        else:
            level = logging.WARNING
        pita.log.init_logging('~/.%s.log' % self.name, level, level)

        self.run(args)

    def usage(self):
        return None

    def run(self, args):
        print 'run not implemented'

class ClientApplication(Application):
    def __init__(self, name):
        Application.__init__(self, name)
        pita.conf.add_option(
            cfg.Option('host',
                       '-H',
                       '--host',
                       ['pita', 'daemon_host'],
                       'localhost'))
